package cn.joeg.app.web.items.user;

import cn.joeg.app.css.util.date.TimeUtil;
import cn.joeg.app.web.cache.UserCacheService;
import cn.joeg.app.web.core.def.AbstractWidgetAdapter;
import cn.joeg.app.web.core.vo.WidgetDataResp;
import cn.joeg.app.web.db.user.UserFightService;
import cn.joeg.app.web.db.user.UserService;
import cn.joeg.app.web.db.util.DBCommon;
import cn.joeg.app.web.items.util.Util;
import cn.joeg.app.web.util.ZipOut;
import com.ecfront.dew.common.Resp;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("user")
public class WidgetAdapter extends AbstractWidgetAdapter {

    private static final String WIDGET_CODE = "user";

    private static final Logger logger = LoggerFactory.getLogger(WidgetAdapter.class);


    @Autowired
    private UserService userService;

    @Autowired
    private UserFightService userFightService;

    @Autowired
    private UserCacheService userCacheService;

    @Autowired
    private UserHelper userHelper;

    @Override
    public String getCode() {
        return WIDGET_CODE;
    }

    /**
     * @param req
     * @return
     */
    public Resp<WidgetDataResp> queryDeviceByAccount(WidgetReqVO req) {
        System.out.println("");
        Map map = new HashMap<>();
        map.put("aa", "aaa");
        return Resp.success(WidgetDataResp.build(map));

    }

    /**
     * login
     *
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> withid(WidgetReqVO.LoginReqVO reqVO) {
        /**
         * 校验
         */
        String deviceid = reqVO.getDeviceid() != null ? reqVO.getDeviceid() : "";
        String mac = reqVO.getMac() != null ? reqVO.getMac() : "";
        String idfa = reqVO.getIdfa() != null ? reqVO.getIdfa() : "";
        //id360 TODO 此处因为class 的属性不能以数字打头 WidgetReqVO.LoginReqVO 对象中 _360id
        String model = reqVO.getModel();
        String type = reqVO.getType();
        int serverid = reqVO.getServerid();
        int ver = reqVO.getVer();
        int result = 0;

        String id360 = reqVO.get_360id() != null ? reqVO.get_360id() : "";
        if (id360 != null) {
            userService._360Login(id360, deviceid);
            deviceid = id360;
        }

        /**
         * 取最大等级公司
         * 取不到创建用户
         */
        List<UserService.UserVO> list = userService.queryUserByDeviceId(null, deviceid);
        UserService.UserVO user = null;
        if (!list.isEmpty()) {
            //存在用户 -> 取公司等级最大的一个
            user = UserHelper.getMaxLevelUser(list, serverid);
        } else {
            //不存在用户 -> 新用户注册
            result = userService.createUser(deviceid, mac, idfa);
            if (result == 0) {
                // 注册失败
                String errorStr = Util.getResultJson(0, WIDGET_CODE + reqVO.getType());
                return Resp.success(WidgetDataResp.build(errorStr));
            }
            // 注册成功
            list = userService.queryUserByDeviceId(null, deviceid);
            if (!list.isEmpty()) {
                user = list.get(0);
            }
        }
        /**
         *
         */
        if (user.getBlocked() == 1) {// 用户被禁
            String errorStr = Util.getResultJson(2, WIDGET_CODE + reqVO.getType());
            return Resp.success(WidgetDataResp.build(errorStr));
        }


        UserFightService.UserFighterVO userFighterVO = userFightService.queryUserFighterByUserid(user.getId());
        String formation = ",\"formation\":";
        if (userFighterVO != null && userFighterVO.getFormation() != null && !"".equals(userFighterVO.getFormation().trim())) {
            formation += "\"" + userFighterVO.getFormation() + "\"";
        } else {
            formation += "\"\"";
        }
        String token = userCacheService.refreshToken(user.getId());
        formation += ",\"token\":" + token;

//        return ZipOut.gzip(user.toJSONAddStr(model + type, formation, ver));
        return null;
    }


    //*********

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> createwithphone(WidgetReqVO.LoginReqVO reqVO) {
        int result = 1;
        String messageType = WIDGET_CODE + reqVO.getType();
        String a = Util.getResultJson(result, messageType);
        return Resp.success(WidgetDataResp.build(a));
    }


    /**
     * 设置电话号码
     *
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setphonepassword(WidgetReqVO.PhonePasswordVO reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();
        Resp resp = userService.setPhone(reqVO.getId(), reqVO.getPhonenum(), reqVO.getPassword());
        int result = 0;
        if (resp.ok()) {
            result = 0;
        } else {
            result = Integer.parseInt(resp.getCode());
        }
        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
    }


    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> changepassword(WidgetReqVO.ChangePasswordVO reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();
        String id = reqVO.getId();
        String phonenum = reqVO.getPhonenum();
        String oldpassword = reqVO.getOldpassword();
        String newpassword = reqVO.getNewpassword();
        int result = userService.changePassword(id, phonenum, oldpassword, newpassword);
        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
    }


    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> newcompany(WidgetReqVO.CompanyVO reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();
        String id = reqVO.getId();
        userService.newCompany(id);
        return Resp.success(WidgetDataResp.build(Util.getResultJson(0, messageType)));
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setsex(WidgetReqVO.SexVO reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();
        String id = reqVO.getId();
        String sex = reqVO.getSex();
        userService.setSex(id, Integer.parseInt(sex));
        return Resp.success(WidgetDataResp.build(Util.getResultJson(0, messageType)));
    }


    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setname(WidgetReqVO.NameVO reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();

        String id = reqVO.getId();
        String name = reqVO.getName();
        int result = userService.setName(id, name);
        return Resp.success(WidgetDataResp.build(Util.getResultJson(0, messageType)));
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setcompanyname(WidgetReqVO.CompanyName reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();
        String id = reqVO.getId();
        String name = reqVO.getCompanyname();
        int result = userService.setCompanyName(id, name);

        if (result == 0) {
//            Card card = new Card();
//            card.initCard(user);
        }

        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setcompanylogo(WidgetReqVO.CompanyName reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }


    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setcompanylogo(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> get(WidgetReqVO.CommonVO reqVO) {
        String messageType = WIDGET_CODE + reqVO.getType();
        UserService.UserVO userVO = userService.queryUserById(null, reqVO.getId());

        UserHelper.tempString(userVO, reqVO.getVer());
        if (userVO.getFansbuffertime() < TimeUtil.nowInt()) {//粉丝BUFFER过期判断
            userVO.setFansbuffer(0);
        }
        return Resp.success(WidgetDataResp.build(userVO));
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> inituser(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getcards(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> oncard(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> offcard(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getitems(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getmaterials(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> all(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getcardsbyid(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> setbattlecards(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> exchange(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> buyactionvalue(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> refresharena(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> createname(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> createway(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getdaily(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> finishdaily(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> dailytalk(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> payrank(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getuserandcards(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> drawvipaward(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getwheel(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> wheel(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> buywheelnum(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getweek(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> getweekaward(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> bossbattle(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }

    /**
     * @param reqVO
     * @return
     */
    public Resp<WidgetDataResp> receivebossaward(WidgetReqVO reqVO) {
//        return Resp.success(WidgetDataResp.build(Util.getResultJson(result, messageType)));
        return null;
    }


}
